package org.vincen.court.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.vincen.court.domain.Reservation;
import org.vincen.court.service.ReservationService;

@SuppressWarnings("deprecation")
public class ReservationFormController extends SimpleFormController {

	private ReservationService reservationService;

	public ReservationFormController() {
		setCommandClass(Reservation.class);
		setCommandName("reservation");
	}

	public void setReservationService(ReservationService reservationService) {
		this.reservationService = reservationService;
	}

	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@Override
	protected void doSubmitAction(Object command) throws Exception {
		super.doSubmitAction(command);
		Reservation reservation = (Reservation) command;
		reservationService.make(reservation);
	}

}
